package com.fengniu.ejb.business.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.fengniu.ejb.business.IDataImportBusinessLocal;
import com.fengniu.ejb.business.IDataImportBusinessRemote;
import com.fengniu.ejb.dao.CategoryDaoLocal;
import com.fengniu.ejb.dao.ContentDaoLocal;
import com.fengniu.ejb.dao.SiteDaoLocal;
import com.fengniu.ejb.dao.UserDaoLocal;
import com.fengniu.ejb.dao.util.IEJBConstants;
import com.fengniu.ejb.entity.Category;
import com.fengniu.ejb.entity.Category2Category;
import com.fengniu.ejb.entity.Content;
import com.fengniu.ejb.entity.Content2Category;
import com.fengniu.ejb.entity.Site;
import com.fengniu.ejb.entity.Site2Category;
import com.fengniu.ejb.entity.Site2Content;
import com.fengniu.ejb.entity.User;
import com.fengniu.ejb.entity.User2Content;
import com.fengniu.ejb.entity.User2Site;
import com.fengniu.ejb.entity.User2User;
import com.tianbing.adlottery.global.IConstants;

@Stateless
@LocalBinding(jndiBinding = IEJBConstants.DataImportBusinessLocalJndiName)
@RemoteBinding(jndiBinding = IEJBConstants.DataImportBusinessRemoteJndiName)
public class DataImportBusinessImpl implements IDataImportBusinessLocal, IDataImportBusinessRemote {
	static Logger logger = Logger.getLogger(DataImportBusinessImpl.class);

	@EJB(name = IEJBConstants.UserDaoLocalJndiName)
	private UserDaoLocal userDao;

	@EJB(name = IEJBConstants.SiteDaoLocalJndiName)
	private SiteDaoLocal siteDao;

	@EJB(name = IEJBConstants.ContentDaoLocalJndiName)
	private ContentDaoLocal contentDao;

	@EJB(name = IEJBConstants.CategoryDaoLocalJndiName)
	private CategoryDaoLocal categoryDao;

	public void importData(Document document) throws DocumentException, InterruptedException {
		treeWalk(document);
	}
	/* (non-Javadoc)
	 * @see com.fengniu.ejb.business.IDataImportBusiness#importData(java.lang.String)
	 */
	public void importData(String xml) throws DocumentException, InterruptedException {
		Document document = DocumentHelper.parseText(xml);
		importData(document);
	}

	private void treeWalk(Document document) throws InterruptedException {
		treeWalk(document.getRootElement());
	}

	private void treeWalk(Element element) throws InterruptedException {
		for (int i = 0, size = element.nodeCount(); i < size; i++) {
			Node node = element.node(i);
			if (node instanceof Element) {
				if (importCategory((Element) node, i)) {
				} else if (importContent((Element) node, i)) {
				} else if (importCategory2Category((Element) node, i)) {
				} else if (importContent2Category((Element) node, i)) {
				} else if (importSite((Element) node, i)) {
				} else if (importSite2Category((Element) node, i)) {
				} else if (importSite2Content((Element) node, i)) {
				} else if (importUser((Element) node, i)) {
				} else if (importUser2User((Element) node, i)) {
				} else if (importUser2Category((Element) node, i)) {
				} else if (importUser2Content((Element) node, i)) {
				} else if (importUser2Site((Element) node, i)) {
				}
				// TODO: check whether child element should be treeWalk
				// for example rss.xml only need to walk to Item will be enough,
				// not
				// need to walk into children of Item
				treeWalk((Element) node);
			} else {
				// do something....
			}
		}
	}

	/**
	 * Sample xml data:- <HonguoziCategory> <!-- Don't isediterof sysId in this
	 * file <ProductGroup sysId="s2020000001000" name="经营范围" /> --> <Category
	 * sysId="s2010000001000" name="各个地区"> <Category sysId="s00001" name="guang
	 * dong" /> <Category sysId="s00002" name="bei jing" /> </Category>
	 * <Category sysId="s2020000001000" name="经营范围" /> </HonguoziCategory>
	 * 
	 * @param element
	 * @return
	 */
	private boolean importCategory(final Element element, final int sequence) {
		if (!element.getName().equals("Category"))
			return false;

		String categoryId = element.attributeValue("categoryId");
		Category pg = categoryDao.findCategoryByCategoryId(categoryId);
		if (pg == null) {
			pg = new Category();
			pg.setCategoryId(categoryId);
		}
		pg.setName(element.attributeValue("name"));
		pg.setFullName(element.attributeValue("fullName"));
		pg.setSmallImageName(element.attributeValue("smallImageName"));

		String type = element.attributeValue("type");
		if (type != null) {
			Integer typeId = IConstants.RelationID.valueOf(IConstants.Type.class, type).getId();
			pg.setTypeId(typeId);
		}
		// pg.setType(element.attributeValue("type"));
		Element parent = element.getParent();
		if (parent != null && parent.attributeValue("categoryId") != null) {
			String parentGroupId = parent.attributeValue("categoryId");
			Category parentGroup = categoryDao.findCategoryByCategoryId(parentGroupId);
			if (parentGroup != null) {
				pg.setPrimaryParent(parentGroup);
			}
		}
		categoryDao.saveCategory(pg);
		return true;
	}

	private boolean importUser(final Element element, final int sequence) {
		if (!element.getName().equals("User"))
			return false;

		String userId = element.attributeValue("userId");

		User user = userDao.findUserByUserId(userId);
		if (user == null) {
			user = new User();
			user.setUserId(userId);
		}
		user.setFirstName(element.attributeValue("firstName"));
		user.setLastName(element.attributeValue("lastName"));
		user.setNickName(element.attributeValue("nickName"));
		user.setPassword(element.attributeValue("password"));
		user.setApiKey(element.attributeValue("apiKey"));
		String type = element.attributeValue("type");
		user.setUserType(type);
		user = userDao.saveUser(user);
		return true;
	}

	private boolean importUser2User(final Element element, final int sequence) {
		// <User2User emailAddr="demo931@500hao.com" relationType="classmate"
		// emailAddr2="demo@500hao.com"/>
		if (!element.getName().equals("User2User"))
			return false;

		String emailAddr = element.attributeValue("userId");
		String relationName = element.attributeValue("relationName");
		String emailAddrTo = element.attributeValue("userId2");
		String comment = element.attributeValue("comment");
		String status = element.attributeValue("status");

		try {
			Integer relationId = IConstants.RelationID.valueOf(IConstants.RelationID.class, relationName).getId();

			User2User u2u = userDao.findUser2UserBy(emailAddr, relationId, null, emailAddrTo);
			if (u2u != null) {
				u2u.setShortDesc(comment);
				userDao.saveUser2User(u2u);
				// tagHelper.prepareTagOfUserRelations(comment, relationType,
				// u2u.getUser(), u2u.getUser2());
			} else {
				User user = userDao.findUserByUserId(emailAddr);
				User user2 = userDao.findUserByUserId(emailAddrTo);
				if (user != null && user2 != null) {
					u2u = new User2User();
					u2u.setShortDesc(comment);
					u2u.setUser(user);
					u2u.setRelationId(relationId);
					u2u.setUser2(user2);
					userDao.saveUser2User(u2u);
					// tagHelper.prepareTagOfUserRelations(comment, rt,
					// u2u.getUser(), u2u.getUser2());
				}
			}
		} catch (Exception e) {
			logger.info("Not Found relationType for relationName=[" + relationName + "]");
		}
		return true;
	}

	private boolean importSite2Site(final Element element, final int sequence) {
		// <Site2Site siteId="010.eetchina.com" relationType="parner"
		// homeUrl2="005.eetchina.com" status="approve" />
		if (!element.getName().equals("Site2Site"))
			return false;

		String siteId = element.attributeValue("siteId");
		String relationType = element.attributeValue("relationType");
		String homeUrl2 = element.attributeValue("homeUrl2");
		String comment = element.attributeValue("comment");
		String status = element.attributeValue("status");

		// Site2Site.RelationType rt =
		// Enum.valueOf(Site2Site.RelationType.class, relationType);
		// Site2Site.Status st = Enum.valueOf(Site2Site.Status.class, status);
		// if (rt != null && st != null) {
		// Site2Site u2u = siteDao.findSite2SiteBy(siteId, rt, homeUrl2);
		// if (u2u != null) {
		// u2u.setShortDesc(comment);
		// u2u.setStatus(st.getCode());
		// siteDao.saveSite2Site(u2u);
		// } else {
		// Site user = siteDao.findSiteBy(siteId);
		// Site g_user2 = siteDao.findSiteBy(homeUrl2);
		// if (user != null && g_user2 != null) {
		// u2u = new Site2Site();
		// u2u.setShortDesc(comment);
		// u2u.setStatus(st.getCode());
		// u2u.setSite(user);
		// u2u.setRelation(rt.getCode());
		// u2u.setSite2(g_user2);
		// siteDao.saveSite2Site(u2u);
		// }
		// }
		// }
		return true;
	}

	/**
	 * 
	 * @param element
	 *            e.g. <Content2Content contentId="001.isbn"
	 *            relationType="iscrosssellof" contentId2="001.isbn"
	 *            status="approve" />
	 * @param sequence
	 * @return
	 */
	private boolean importContent2Content(final Element element, final int sequence) {

		if (!element.getName().equals("Content2Content"))
			return false;

		String contentId = element.attributeValue("contentId");
		String relationType = element.attributeValue("relationType");
		String contentId2 = element.attributeValue("contentId2");
		String comment = element.attributeValue("comment");
		String status = element.attributeValue("status");

		// Content2Content.RelationType rt =
		// Enum.valueOf(Content2Content.RelationType.class, relationType);
		// Content2Content.Status st =
		// Enum.valueOf(Content2Content.Status.class, status);
		// if (rt != null && st != null) {
		// Content2Content.RelationType[] relationTypes = { rt };
		// Content2Content c2c = contentDao.findContent2ContentBy(contentId,
		// relationTypes, contentId2, st);
		// if (c2c != null) {
		// c2c.setShortDesc(comment);
		// c2c.setStatus(st.getCode());
		// contentDao.saveContent2Content(c2c);
		// } else {
		// Content coupon = contentDao.findContentBy(contentId);
		// Content g_article2 = contentDao.findContentBy(contentId2);
		// if (coupon != null && g_article2 != null) {
		// c2c = new Content2Content();
		// c2c.setShortDesc(comment);
		// c2c.setStatus(st.getCode());
		// c2c.setContent(coupon);
		// c2c.setRelation(rt.getCode());
		// c2c.setContent2(g_article2);
		// contentDao.saveContent2Content(c2c);
		// }
		// }
		// }
		return true;
	}

	private boolean importCoupon2Content(final Element element, final int sequence) {

		if (!element.getName().equals("Coupon2Content"))
			return false;

		String couponId = element.attributeValue("couponId");
		String relationType = element.attributeValue("relationType");
		String contentId = element.attributeValue("contentId");
		String comment = element.attributeValue("comment");
		String status = element.attributeValue("status");

		// Coupon2Content.RelationType rt =
		// Enum.valueOf(Coupon2Content.RelationType.class, relationType);
		// Coupon2Content.Status st = Enum.valueOf(Coupon2Content.Status.class,
		// status);
		// if (rt != null && st != null) {
		// Coupon2Content.RelationType[] relationTypes = { rt };
		// Coupon2Content p2c = couponDao.findCoupon2ContentBy(couponId,
		// relationTypes, contentId, st);
		// if (p2c != null) {
		// p2c.setShortDesc(comment);
		// p2c.setStatus(st.getCode());
		// couponDao.save(p2c);
		// } else {
		// Coupon coupon = couponDao.findCouponBy(couponId);
		// Content coupon = contentDao.findContentBy(contentId);
		// if (coupon != null && coupon != null) {
		// p2c = new Coupon2Content();
		// p2c.setShortDesc(comment);
		// p2c.setStatus(st.getCode());
		// p2c.setCoupon(coupon);
		// p2c.setRelation(rt.getCode());
		// p2c.setContent(coupon);
		// couponDao.save(p2c);
		// }
		// }
		// }
		return true;
	}

	// private boolean importSiteFoodStore(final Element element, final int
	// sequence) {
	// if (!element.getName().equals("SiteFoodStore"))
	// return false;
	//
	// String siteId = element.attributeValue("siteId");
	// SiteFoodStore webSite = siteDao.findFoodStoreBy(siteId);
	// if (webSite == null) {
	// webSite = new SiteFoodStore();
	// webSite.setHomeUrl(siteId);
	// }
	// webSite.setName(element.attributeValue("name"));
	// webSite.setFullName(element.attributeValue("fullName"));
	// siteDao.save(webSite);
	// return true;
	// }

	private boolean importContent2Category(final Element element, final int sequence) {
		if (!element.getName().equals("Content2Category"))
			return false;

		String contentId = element.attributeValue("contentId");
		String relationName = element.attributeValue("relationName");
		String categoryId = element.attributeValue("categoryId");
		String comment = element.attributeValue("comment");
		String status = element.attributeValue("status");

		Integer relationId = IConstants.RelationID.valueOf(IConstants.RelationID.class, relationName).getId();

		Content2Category c2g = contentDao.findContent2CategoryBy(contentId, relationId, categoryId);
		if (c2g != null) {
			c2g.setShortDesc(comment);
			contentDao.saveContent2Category(c2g);
		} else {
			Content site = contentDao.findContentByContentId(contentId);

			Category category = categoryDao.findCategoryByCategoryId(categoryId);
			if (site != null && category != null) {
				c2g = new Content2Category();
				c2g.setContent(site);
				c2g.setRelationId(relationId);
				c2g.setCategory(category);
				contentDao.saveContent2Category(c2g);
			} else if (site == null) {
				logger.info("Not Found Content for contentId=[" + contentId + "]");
			} else if (category == null) {
				logger.info("Not Found Category for id=[" + categoryId + "]");
			}
		}

		return true;
	}

	private boolean importSite2Category(final Element element, final int sequence) {
		if (!element.getName().equals("Site2Category"))
			return false;

		String siteId = element.attributeValue("siteId");
		String relationName = element.attributeValue("relationName");
		String categoryId = element.attributeValue("categoryId");
		String comment = element.attributeValue("comment");
		String status = element.attributeValue("status");
		Integer relationId = IConstants.RelationID.valueOf(IConstants.RelationID.class, relationName).getId();

		Site2Category s2g = siteDao.findSite2CategoryBy(siteId, relationId, categoryId, null);
		if (s2g != null) {
			s2g.setShortDesc(comment);
			siteDao.saveSite2Category(s2g);
		} else {
			Site site = siteDao.findSiteBySiteId(siteId);

			Category category = categoryDao.findCategoryByCategoryId(categoryId);
			if (site != null && category != null) {
				s2g = new Site2Category();
				s2g.setSite(site);
				s2g.setRelationId(relationId);
				s2g.setCategory(category);
				siteDao.saveSite2Category(s2g);
			} else if (site == null) {
				logger.info("Not Found Site for siteId=[" + siteId + "]");
			} else if (category == null) {
				logger.info("Not Found Category for id=[" + categoryId + "]");
			}
		}

		return true;
	}

	private boolean importUser2Site(final Element element, final int sequence) {
		if (!element.getName().equals("User2Site"))
			return false;

		String userId = element.attributeValue("userId");
		String relationName = element.attributeValue("relationName");
		String siteId = element.attributeValue("siteId");
		String comment = element.attributeValue("comment");
		String status = element.attributeValue("status");
		try {
			Integer relationId = IConstants.RelationID.valueOf(IConstants.RelationID.class, relationName).getId();

			User2Site u2s = userDao.findUser2SiteBy(userId, relationId, siteId);
			if (u2s != null) {
				u2s.setShortDesc(comment);
				userDao.saveUser2Site(u2s);
			} else {
				User user = userDao.findUserByUserId(userId);
				Site site = siteDao.findSiteBySiteId(siteId);
				if (user != null && site != null) {
					u2s = new User2Site();
					u2s.setUser(user);
					u2s.setRelationId(relationId);
					u2s.setSite(site);
					u2s.setShortDesc(comment);
					userDao.saveUser2Site(u2s);
				} else if (site == null) {
					logger.info("Not Found Site for siteId=[" + siteId + "]");
				} else if (user == null) {
					logger.info("Not Found User for userId=[" + userId + "]");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	// private boolean importUser2SiteOrder(final Element element, final int
	// sequence) {
	// // <User2SiteOrder orderId="001.orderfood" emailAddr="demo001@500hao.com"
	// // relationType="orderFood" siteId="001.foodstore.com"
	// discountAmount="108"
	// // status="approve">
	// // <User2SiteOrderItem contentId="001.food" contentUnitPrice="18"
	// // contentQuantity="1"/>
	// // <User2SiteOrderItem contentId="002.food" contentUnitPrice="28"
	// // contentQuantity="2"/>
	// // <User2SiteOrderItem contentId="003.food" contentUnitPrice="38"
	// // contentQuantity="3"/>
	// // <User2SiteOrderItem contentId="004.food" contentUnitPrice="48"
	// // contentQuantity="4"/>
	// // <User2SiteOrderItem contentId="005.food" contentUnitPrice="58"
	// // contentQuantity="5"/>
	// // </User2SiteOrder>
	//
	// if (!element.getName().equals("User2SiteOrder"))
	// return false;
	//
	// String orderId = element.attributeValue("orderId");
	// String emailAddr = element.attributeValue("emailAddr");
	// String relationType = element.attributeValue("relationType");
	// String siteId = element.attributeValue("siteId");
	// String status = element.attributeValue("status");
	// User2Site.RelationType rt = Enum.valueOf(User2Site.RelationType.class,
	// relationType);
	// User2SiteOrder.Status st = Enum.valueOf(User2SiteOrder.Status.class,
	// status);
	// if (rt != null && st != null) {
	// User2SiteOrder u2u = userDao.findUser2SiteOrderBy(orderId);
	// if (u2u != null) {
	// u2u.setStatus(st.getCode());
	// u2u.setRelationType(rt.getCode());
	// u2u.setStatus(status);
	// userDao.saveUser2Site(u2u);
	// } else {
	// u2u = new User2SiteOrder();
	// User user = userDao.findUserByEmail(emailAddr);
	// Site site = siteDao.findSiteBy(siteId);
	// u2u.setOrderId(orderId);
	// u2u.setUser(user);
	// u2u.setRelationType(rt.getCode());
	// u2u.setStatus(st.getCode());
	// u2u.setSite(site);
	// u2u.setOrderItems(null);
	// if (user != null && site != null) {
	// u2u = (User2SiteOrder) userDao.saveUser2Site(u2u);
	// List<User2SiteOrderItem> orderItems =
	// prepareOrderItems(element.elements(), u2u);
	// u2u.setOrderItems(orderItems);
	// u2u = (User2SiteOrder) userDao.saveUser2Site(u2u);
	// }
	// }
	// }
	//
	// return true;
	// }
	//
	// private List<User2SiteOrderItem> prepareOrderItems(List elements,
	// User2SiteOrder order) {
	// List<User2SiteOrderItem> result = new ArrayList<User2SiteOrderItem>(0);
	// Iterator it = elements.iterator();
	// while (it.hasNext()) {
	// Element e = (Element) it.next();
	// if (!e.getName().equals("User2SiteOrderItem"))
	// continue;
	// // <User2SiteOrderItem contentId="001.food" contentUnitPrice="18"
	// // contentQuantity="1"/>
	// String contentId = e.attributeValue("contentId");
	// String contentUnitPrice = e.attributeValue("contentUnitPrice");
	// String contentQuantity = e.attributeValue("contentQuantity");
	// Content coupon = contentDao.findContentBy(contentId);
	// if (coupon != null) {
	// User2SiteOrderItem orderItem = new User2SiteOrderItem();
	// orderItem.setContent(coupon);
	// orderItem.setContentUnitPrice(Double.valueOf(contentUnitPrice));
	// orderItem.setContentQuantity(Double.valueOf(contentQuantity));
	// orderItem.setOrderHeader(order);
	// result.add(orderItem);
	// }
	// }
	// return result;
	// }

	private boolean importUser2Content(final Element element, final int sequence) {
		if (!element.getName().equals("User2Content"))
			return false;

		String userId = element.attributeValue("userId");
		String relationName = element.attributeValue("relationName");
		String contentId = element.attributeValue("contentId");
		String comment = element.attributeValue("comment");
		String status = element.attributeValue("status");

		try {
			Integer relationId = IConstants.RelationID.valueOf(IConstants.RelationID.class, relationName).getId();

			User2Content u2c = userDao.findUser2ContentBy(userId, relationId, contentId);
			if (u2c != null) {
				u2c.setShortDesc(comment);
				userDao.saveUser2Content(u2c);
			} else {
				User user = userDao.findUserByUserId(userId);
				Content content = contentDao.findContentByContentId(contentId);
				if (user != null && content != null) {
					// 0.create User2Content
					u2c = new User2Content();
					u2c.setUser(user);
					u2c.setRelationId(relationId);
					u2c.setContent(content);
					u2c.setShortDesc(comment);
					userDao.saveUser2Content(u2c);
				}
			}
		} catch (Exception e) {
			logger.info("Not Found relationType for relationName=[" + relationName + "]");
		}

		return true;
	}

	private boolean importSite2Content(final Element element, final int sequence) {
		if (!element.getName().equals("Site2Content"))
			return false;

		String siteId = element.attributeValue("siteId");
		String relationName = element.attributeValue("relationName");
		String contentId = element.attributeValue("contentId");
		String shortDesc = element.elementText("shortDesc");
		String longDesc = element.elementText("longDesc");
		String listPrice = element.attributeValue("listPrice");
		String memberPrice = element.attributeValue("memberPrice");
		String status = element.attributeValue("status");

		Integer relationId = IConstants.RelationID.valueOf(IConstants.RelationID.class, relationName).getId();

		Site2Content s2c = siteDao.findSite2ContentBy(siteId, relationId, contentId);
		if (s2c != null) {
			s2c.setShortDesc(shortDesc);
			s2c.setLongDesc(longDesc);
			s2c.setListPrice(new Double(listPrice));
			s2c.setMemberPrice(new Double(memberPrice));
			siteDao.saveSite2Content(s2c);
		} else {
			Site site = siteDao.findSiteBySiteId(siteId);
			Content content = contentDao.findContentByContentId(contentId);
			if (site != null && content != null) {
				s2c = new Site2Content();
				s2c.setSite(site);
				s2c.setRelationId(relationId);
				s2c.setContent(content);
				s2c.setListPrice(new Double(listPrice));
				s2c.setMemberPrice(new Double(memberPrice));
				s2c.setShortDesc(shortDesc);
				s2c.setLongDesc(longDesc);
				siteDao.saveSite2Content(s2c);
			} else if (site == null) {
				logger.info("Not Found Site for siteId=[" + siteId + "]");
			} else if (content == null) {
				logger.info("Not Found Content for contentId=[" + contentId + "]");
			}
		}
		return true;
	}

	// <User2Category emailAddr="demo001@500hao.com"
	// relationType="platinummember"
	// id="cn"/>
	private boolean importUser2Category(final Element element, final int sequence) {
		if (!element.getName().equals("User2Category"))
			return false;

		String emailAddr = element.attributeValue("userId");
		String relationName = element.attributeValue("relationName");
		String categoryId = element.attributeValue("categoryId");
		String comment = element.attributeValue("comment");
		String status = element.attributeValue("status");

		// User2Category.RelationType rt =
		// Enum.valueOf(User2Category.RelationType.class, relationType);
		// User2Category.Status st = Enum.valueOf(User2Category.Status.class,
		// status);
		// if (rt != null && st != null) {
		// User2Category.RelationType[] u2cRelationTypes = {rt};
		// User2Category u2u = userDao.findUser2CategoryBy(emailAddr,
		// u2cRelationTypes, id);
		// if (u2u != null) {
		// u2u.setShortDesc(comment);
		// u2u.setStatus(st.getCode());
		// userDao.saveUser2Category(u2u);
		// } else {
		// u2u = new User2Category();
		// User user = userDao.findUserByUserId(emailAddr);
		// Category category = categoryDao.findCategoryBy(id);
		// u2u.setUser(user);
		// u2u.setRelation(rt.getCode());
		// u2u.setStatus(st.getCode());
		// u2u.setCategory(category);
		// if (user != null && category != null) {
		// userDao.saveUser2Category(u2u);
		// }
		// }
		// }
		return true;
	}

	// private boolean importUserContactAddress(final Element element, final int
	// sequence) {
	// if (!element.getName().equals("UserContactAddress"))
	// return false;
	// String emailAddr = element.attributeValue("emailAddr");
	// User user = userDao.findUserByEmail(emailAddr);
	//
	// ContactAddress c = new ContactAddress();
	// c.setAddress1(element.attributeValue("address1"));
	// c.setAddress2(element.attributeValue("address2"));
	// c.setCity(element.attributeValue("city"));
	// c.setCountry(element.attributeValue("country"));
	// c.setFax(element.attributeValue("fax"));
	// c.setMobile(element.attributeValue("mobile"));
	// c.setPersonDepartment(element.attributeValue("personDepartment"));
	// c.setPersonName(element.attributeValue("personName"));
	// c.setPersonTitle(element.attributeValue("personTitle"));
	// c.setState(element.attributeValue("state"));
	// c.setUser(user);
	// userDao.saveContactAddress(user.getSysId(), c);
	// return true;
	// }
	//
	// private boolean importUserContactIMNumber(final Element element, final
	// int sequence) {
	// if (!element.getName().equals("UserContactIMNumber"))
	// return false;
	//
	// String emailAddr = element.attributeValue("emailAddr");
	// User user = userDao.findUserByEmail(emailAddr);
	//
	// ContactIMNumber c = new ContactIMNumber();
	// c.setNumber(element.attributeValue("number"));
	// c.setType(element.attributeValue("type"));
	// c.setUser(user);
	// userDao.saveContactIMNumber(user.getSysId(), c);
	// return true;
	// }

	private boolean importSystemProperty(final Element element, final int sequence) {
		if (!element.getName().equals("SystemProperty"))
			return false;

		// String propertyId = element.attributeValue("propertyId");
		// String value = element.attributeValue("value");
		// String desc = element.attributeValue("desc");
		// SystemProperty sp = systemDao.findSystemPropertyBy(propertyId);
		// if (sp == null) {
		// sp = new SystemProperty();
		// sp.setPropertyId(propertyId);
		// }
		// sp.setValue(value);
		// sp.setLongDesc(desc);
		// systemDao.save(sp);
		return true;
	}

	private boolean importSite(final Element element, final int sequence) {
		if (!element.getName().equals("Site"))
			return false;

		String siteId = element.attributeValue("siteId");
		String name = element.attributeValue("name");
		String type = element.attributeValue("type");
		int integral = 0;
		try {
			integral = element.attributeValue("integral") == null ? 0 : Integer.parseInt(element
					.attributeValue("integral"));
		} catch (NumberFormatException e) {
			integral = 0;
		}
		String shortDesc = element.elementText("shortDesc");
		String longDesc = element.elementText("longDesc");

		Site site = siteDao.findSiteBySiteId(siteId);
		if (site == null) {
			site = new Site();
			site.setSiteId(siteId);
		}

		Integer typeId = IConstants.RelationID.valueOf(IConstants.Type.class, type).getId();

		site.setName(name);
		site.setTypeId(typeId);
		site.setIntegral(integral);
		site.setShortDesc(shortDesc);
		site.setLongDesc(longDesc);
		siteDao.saveSite(site);
		return true;
	}

	private boolean importContent(final Element element, final int sequence) {
		if (!element.getName().equals("Content"))
			return false;

		String siteId = element.attributeValue("contentId");
		String name = element.attributeValue("name");
		String fullName = element.attributeValue("fullName");
		String type = element.attributeValue("type");
		int integral = 0;
		try {
			integral = element.attributeValue("integral") == null ? 0 : Integer.parseInt(element
					.attributeValue("integral"));
		} catch (NumberFormatException e) {
			integral = 0;
		}
		String smallImage = element.attributeValue("smallImage");
		String mediumImage = element.attributeValue("mediumImage");
		String largeImage = element.attributeValue("largeImage");

		String mainFeature = element.elementText("mainFeature");
		String shortDesc = element.elementText("shortDesc");
		String longDesc = element.elementText("longDesc");

		Content content = contentDao.findContentByContentId(siteId);
		if (content == null) {
			content = new Content();
			content.setContentId(siteId);
		}
		if (name != null && !name.trim().equals(""))
			content.setName(name);
		if (fullName != null && !fullName.trim().equals(""))
			content.setFullName(fullName);

		if (type != null && !name.trim().equals("")) {
			Integer typeId = IConstants.RelationID.valueOf(IConstants.Type.class, type).getId();
			content.setTypeId(typeId);
		}
		content.setIntegral(integral);
		if (smallImage != null && !smallImage.trim().equals(""))
			content.setSmallImage(smallImage);
		if (mediumImage != null && !mediumImage.trim().equals(""))
			content.setMediumImage(mediumImage);
		if (largeImage != null && !largeImage.trim().equals(""))
			content.setLargeImage(largeImage);

		if (mainFeature != null && !mainFeature.trim().equals(""))
			content.setMainFeature(mainFeature);
		if (shortDesc != null && !shortDesc.trim().equals(""))
			content.setShortDesc(shortDesc);
		if (longDesc != null && !longDesc.trim().equals(""))
			content.setLongDesc(longDesc);
		contentDao.saveContent(content);
		return true;
	}

	private boolean importContentCoupon(final Element element, final int sequence) {
		if (!element.getName().equals("Coupon"))
			return false;

		String couponId = element.attributeValue("couponId");
		String name = element.attributeValue("name");
		String desc = element.attributeValue("desc");
		String status = element.attributeValue("status");

		String siteId = element.attributeValue("siteId");

		// Content.Status st = Enum.valueOf(Content.Status.class, status);
		// if (st != null) {
		// Coupon sp = couponDao.findCouponBy(couponId);
		// Coupon cb = null;
		// if (sp instanceof Coupon) {
		// cb = (Coupon) sp;
		// }
		//
		// if (cb == null) {
		// cb = new Coupon();
		// cb.setCouponId(couponId);
		// }
		// cb.setName(name);
		// cb.setLongDesc(desc);
		// cb.setStatus(st.getCode());
		// cb.setContentType(Content.Type.coupon.getCode());
		// Site site = siteDao.findSiteBy(siteId);
		// cb.setSite(site);
		// couponDao.save(cb);
		// }
		return true;
	}

	private boolean importCouponItem(final Element element, final int sequence) {
		if (!element.getName().equals("CouponItem"))
			return false;

		String couponItemId = element.attributeValue("couponItemId");
		String name = element.attributeValue("name");
		String desc = element.attributeValue("desc");
		String status = element.attributeValue("status");
		String earnedValue = element.attributeValue("earnedValue");
		String lotteryNum = element.attributeValue("lotteryNum");
		String validStartDate = element.attributeValue("validStartDate");
		String validEndDate = element.attributeValue("validEndDate");

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date validStartDate1 = new Date();
		try {
			validStartDate1 = df.parse(validStartDate);
		} catch (ParseException e) {
		}
		Date validEndDate1 = new Date();
		try {
			validEndDate1 = df.parse(validEndDate);
		} catch (ParseException e) {
		}

		String couponId = element.attributeValue("couponId");
		String emailAddr = element.attributeValue("emailAddr");
		//
		// Content.Status st = Enum.valueOf(Content.Status.class, status);
		// if (st != null) {
		// Coupon sp = couponDao.findCouponBy(couponId);
		// if (sp != null) {
		// CouponItem ci = couponDao.findCouponItemBy(couponItemId);
		//
		// if (ci == null) {
		// ci = new CouponItem();
		// ci.setCouponItemId(couponItemId);
		// }
		// ci.setCoupon(sp);
		// ci.setName(name);
		// ci.setLongDesc(desc);
		// ci.setStatus(st.getCode());
		// ci.setEarnedValue(Integer.valueOf(earnedValue));
		// ci.setValidStartDate(validStartDate1);
		// ci.setValidEndDate(validEndDate1);
		// ci.setLotteryNum(lotteryNum);
		// User user = userDao.findUserByUserId(emailAddr);
		// ci.setUser(user);
		// couponDao.save(ci);
		// }
		// }
		return true;
	}

	private boolean importCategory2Category(final Element element, final int sequence) {
		if (!element.getName().equals("Category2Category"))
			return false;

		String categoryId = element.attributeValue("categoryId");
		String relationName = element.attributeValue("relationName");
		String categoryId2 = element.attributeValue("categoryId2").toLowerCase();
		String comment = element.attributeValue("comment");
		String status = element.attributeValue("status");
		Integer relationId = IConstants.RelationID.valueOf(IConstants.RelationID.class, relationName).getId();
		Integer[] relationIds = { relationId };
		List<Category2Category> g2gs = categoryDao.getListCategory2CategoryBy(categoryId, relationIds, categoryId2,
				null, null, 0, 1);
		Category2Category g2g = null;
		if (g2gs != null && g2gs.size() > 0) {
			g2g = g2gs.get(0);
			g2g.setShortDesc(comment);
			categoryDao.saveCategory2Category(g2g);
		} else {
			Category category = categoryDao.findCategoryByCategoryId(categoryId);
			Category category2 = categoryDao.findCategoryByCategoryId(categoryId2);
			if (category != null && category != null) {
				g2g = new Category2Category();
				g2g.setCategory(category);
				g2g.setRelationId(relationId);
				g2g.setCategory2(category2);
				categoryDao.saveCategory2Category(g2g);
			} else if (category == null) {
				logger.info("Not Found Category for categoryId=[" + categoryId + "]");
			} else if (category2 == null) {
				logger.info("Not Found Category for categoryId2=[" + categoryId2 + "]");
			}
		}

		return true;
	}


}
